<?php
$haveResearchProject = count($researchProjectForm->employeeResearchProjects) > 0;
?>

<a name="researchProject"></a>
<!-- this is research project section -->
<?php if ($employeeResearchProjectPermissions->canCreate() || ($haveResearchProject && $employeeResearchProjectPermissions->canUpdate())) { ?>
    <div id="changeResearchProject">
        <div class="head">
            <h1 id="headChangeResearchProject"><?php echo __('Add Research Project'); ?></h1>
        </div>

        <div class="inner">
            <form id="frmResearchProject" action="<?php echo url_for('pim/saveDeleteResearchProject?empNumber=' .
                $empNumber . "&option=save"); ?>" method="post">
                <?php echo $researchProjectForm['_csrf_token']; ?>
                <?php echo $researchProjectForm['emp_number']->render(); ?>
                <?php echo $researchProjectForm["seqno"]->render(); ?>

                <fieldset>
                    <ol>
                        <li>
                            <?php echo $researchProjectForm['name']->renderLabel(__('Research Project Name') . ' <em>*</em>'); ?>
                            <?php echo $researchProjectForm['name']->render(array("class" => "formInputText", "maxlength" => 255)); ?>
                        </li>
                        <li>
                            <?php echo $researchProjectForm['role']->renderLabel(__('Research Project Role') . ' <em>*</em>'); ?>
                            <?php echo $researchProjectForm['role']->render(array("class" => "formInputText", "maxlength" => 50)); ?>
                        </li>
                        <li>
                            <?php echo $researchProjectForm['start_date']->renderLabel(__('Research Project Start Date') . ' <em>*</em>'); ?>
                            <?php echo $researchProjectForm['start_date']->render(array("class" => "formInputText")); ?>
                        </li>
                        <li>
                            <?php echo $researchProjectForm['end_date']->renderLabel(__('Research Project End Date')); ?>
                            <?php echo $researchProjectForm['end_date']->render(array("class" => "formInputText")); ?>
                        </li>
                        <li>
                            <?php echo $researchProjectForm['status']->renderLabel(__('Research Project Status')); ?>
                            <?php echo $researchProjectForm['status']->render(array("class" => "formInputText", "maxlength" => 50)); ?>
                        </li>
                        <li class="required">
                            <em>*</em> <?php echo __(CommonMessages::REQUIRED_FIELD); ?>
                        </li>
                    </ol>
                    <p>
                        <input type="button" class="" id="btnResearchProjectSave" value="<?php echo __("Save"); ?>"/>
                        <?php if ((!$haveResearchProject) ||
                            ($haveResearchProject && $employeeResearchProjectPermissions->canCreate()) ||
                            ($haveResearchProject && $employeeResearchProjectPermissions->canUpdate())
                        ) {
                            ?>
                            <input type="button" class="reset" id="btnResearchProjectCancel"
                                   value="<?php echo __("Cancel"); ?>"/>
                        <?php } ?>
                    </p>
                </fieldset>
            </form>
        </div>
    </div> <!-- changeResearchProject  -->
<?php } ?>

<div class="miniList" id="sectionResearchProject">

    <div class="head">
        <h1><?php echo __("Research Project"); ?></h1>
    </div>

    <div class="inner">

        <?php if ($employeeResearchProjectPermissions->canRead()) : ?>

            <?php include_partial('global/flash_messages', array('prefix' => 'researchProject')); ?>

            <form id="frmDelResearchProject" action="<?php echo url_for('pim/saveDeleteResearchProject?empNumber=' .
                $empNumber . "&option=delete"); ?>" method="post">
                <p id="actionResearchProject">
                    <?php if ($employeeResearchProjectPermissions->canCreate()) { ?>
                        <input type="button" value="<?php echo __("Add"); ?>" class="" id="addResearchProject"/>
                    <?php } ?>
                    <?php if ($employeeResearchProjectPermissions->canDelete()) { ?>
                        <input type="button" value="<?php echo __("Delete"); ?>" class="delete"
                               id="delResearchProject"/>
                    <?php } ?>
                </p>
                <table id="" class="table hover">
                    <thead>
                    <tr>
                        <?php if ($employeeResearchProjectPermissions->canDelete()) { ?>
                            <th class="check" style="width:2%"><input type="checkbox" id="researchProjectCheckAll"/>
                            </th>
                        <?php } ?>
                        <th><?php echo __('Research Project Name'); ?></th>
                        <th><?php echo __('Research Project Role'); ?></th>
                        <th><?php echo __('Research Project Start Date'); ?></th>
                        <th><?php echo __('Research Project End Date'); ?></th>
                        <th><?php echo __('Research Project Status'); ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php if (!$haveResearchProject) { ?>
                        <tr>
                            <?php if ($employeeResearchProjectPermissions->canDelete()) { ?>
                                <td class="check"></td>
                            <?php } ?>
                            <td><?php echo __(TopLevelMessages::NO_RECORDS_FOUND); ?></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    <?php } else { ?>
                        <?php
                        $researchProjects = $researchProjectForm->employeeResearchProjects;
                        $row = 0;
                        foreach ($researchProjects as $researchProject) :
                            $cssClass = ($row % 2) ? 'even' : 'odd';
                            $startDate = set_datepicker_date_format($researchProject->start_date);
                            $endDate = set_datepicker_date_format($researchProject->end_date);
                            ?>
                            <tr class="<?php echo $cssClass; ?>">
                                <td class="check">
                                    <input type="hidden" id="name_<?php echo $researchProject->seqno; ?>"
                                           value="<?php echo htmlspecialchars($researchProject->name); ?>"/>
                                    <input type="hidden" id="role_<?php echo $researchProject->seqno; ?>"
                                           value="<?php echo htmlspecialchars($researchProject->role); ?>"/>
                                    <input type="hidden" id="startDate_<?php echo $researchProject->seqno; ?>"
                                           value="<?php echo $startDate; ?>"/>
                                    <input type="hidden" id="endDate_<?php echo $researchProject->seqno; ?>"
                                           value="<?php echo $endDate; ?>"/>
                                    <input type="hidden" id="status_<?php echo $researchProject->seqno; ?>"
                                           value="<?php echo htmlspecialchars($researchProject->status); ?>"/>
                                    <?php if ($employeeResearchProjectPermissions->canDelete()) { ?>
                                        <input type="checkbox" class="chkbox1"
                                               value="<?php echo $researchProject->seqno; ?>"
                                               name="delResearchProject[]"/>
                                    <?php } else { ?>
                                        <input type="hidden" class="chkbox1"
                                               value="<?php echo $researchProject->seqno; ?>"
                                               name="delResearchProject[]"/>
                                    <?php } ?>
                                </td>
                                <td class="name">
                                    <?php if ($employeeResearchProjectPermissions->canUpdate()) { ?>
                                        <a class="edit"
                                           href="#"><?php echo htmlspecialchars($researchProject->name); ?></a>
                                    <?php
                                    } else {
                                        echo htmlspecialchars($researchProject->name);
                                    }
                                    ?>
                                </td>
                                <td><?php echo htmlspecialchars($researchProject->role); ?></td>
                                <td><?php echo $startDate; ?></td>
                                <td><?php echo $endDate; ?></td>
                                <td><?php echo htmlspecialchars($researchProject->status); ?></td>
                            </tr>
                            <?php $row++;
                        endforeach;
                    } ?>
                    </tbody>
                </table>
            </form>

        <?php else : ?>
            <div><?php echo __(CommonMessages::DONT_HAVE_ACCESS); ?></div>
        <?php endif; ?>

    </div>

</div> <!-- miniList-sectionResearchProject -->

<script type="text/javascript">
    //<![CDATA[

    var lang_addResearchProject = "<?php echo __('Add Research Project'); ?>";
    var lang_editResearchProject = "<?php echo __('Edit Research Project'); ?>";
    var lang_nameRequired = "<?php echo __(ValidationMessages::REQUIRED); ?>";
    var lang_roleRequired = "<?php echo __(ValidationMessages::REQUIRED); ?>";
    var lang_startDateRequired = "<?php echo __(ValidationMessages::REQUIRED); ?>";
    var lang_exceed255Chars = "<?php echo __(ValidationMessages::TEXT_LENGTH_EXCEEDS, array('%amount%' => 255)); ?>";
    var lang_exceed50Chars = "<?php echo __(ValidationMessages::TEXT_LENGTH_EXCEEDS, array('%amount%' => 50)); ?>";
    var lang_invalidDate = '<?php echo __(ValidationMessages::DATE_FORMAT_INVALID,
            array('%format%' => str_replace('yy', 'yyyy', get_datepicker_date_format($sf_user->getDateFormat())))); ?>';
    var lang_startDateLessEndDate = "<?php echo __('End date should be after Start date'); ?>";
    var lang_selectResearchProjectToDelete = "<?php echo __(TopLevelMessages::SELECT_RECORDS); ?>";
    var datepickerDateFormat = '<?php echo get_datepicker_date_format($sf_user->getDateFormat()); ?>';
    var canEdit = '<?php echo $employeeResearchProjectPermissions->canUpdate(); ?>';

    //]]>
</script>

<script type="text/javascript">
//<![CDATA[

$(document).ready(function () {
    //--this section is for research project

    var startDate = "";

    function addEditLinks() {
        // called here to avoid double adding links - When in edit mode and cancel is pressed.
        removeEditLinks();
        $('form#frmDelResearchProject table tbody td.name').wrapInner('<a class="edit" href="#"/>');
    }

    function removeEditLinks() {
        $('form#frmDelResearchProject table tbody td.name a').each(function (index) {
            $(this).parent().text($(this).text());
        });
    }

    //hide add research project section
    $("#changeResearchProject").hide();
    $("#researchProjectRequiredNote").hide();

    //hiding the data table if records are not available
    if ($(".chkbox1").length == 0) {
        $('div#sectionResearchProject .check').hide();
        $("#editResearchProject").hide();
        $("#delResearchProject").hide();
    }

    //if check all button clicked
    $("#researchProjectCheckAll").click(function () {
        $(".chkbox1").removeAttr("checked");
        if ($("#researchProjectCheckAll").attr("checked")) {
            $(".chkbox1").attr("checked", "checked");
        }
    });

    //remove tick from the all button if any checkbox unchecked
    $(".chkbox1").click(function () {
        $("#researchProjectCheckAll").removeAttr('checked');
        if ($(".chkbox1").length == $(".chkbox1:checked").length) {
            $("#researchProjectCheckAll").attr('checked', 'checked');
        }
    });

    $("#addResearchProject").click(function () {

        removeEditLinks();
        clearMessageBar();

        //changing the headings
        $("#headChangeResearchProject").text(lang_addResearchProject);
        $(".chkbox1").hide();
        $("#researchProjectCheckAll").hide();

        //hiding action button section
        $("#actionResearchProject").hide();

        $('div#changeResearchProject label.error').hide();

        $("#employeeResearchProject_seqno").val("");
        $("#employeeResearchProject_name").val("");
        $("#employeeResearchProject_role").val("");
        $("#employeeResearchProject_start_date").val(displayDateFormat);
        $("#employeeResearchProject_end_date").val(displayDateFormat);
        $("#employeeResearchProject_status").val("");

        //show add  employeeResearchProject form
        $("#changeResearchProject").show();
        $("#researchProjectRequiredNote").show();
    });

    //clicking of delete button
    $("#delResearchProject").click(function () {

        clearMessageBar();

        if ($(".chkbox1:checked").length > 0) {
            $("#frmDelResearchProject").submit();
        } else {
            $("#researchProjectMessagebar").attr('class', 'messageBalloon_notice').text(lang_selectResearchProjectToDelete);
        }

    });

    $("#btnResearchProjectSave").click(function () {
        clearMessageBar();
        startDate = $('#employeeResearchProject_start_date').val();
        $("#frmResearchProject").submit();
    });

    //form validation
    var researchProjectValidator =
        $("#frmResearchProject").validate({
            rules: {
                'employeeResearchProject[name]': {
                    required: true,
                    maxlength: 255
                },
                'employeeResearchProject[role]': {
                    required: true,
                    maxlength: 50
                },
                'employeeResearchProject[start_date]': {
                    valid_date: function () {
                        return {
                            format: datepickerDateFormat,
                            required: true,
                            displayFormat: displayDateFormat
                        }
                    }
                },
                'employeeResearchProject[end_date]': {
                    valid_date: function () {
                        return {
                            format: datepickerDateFormat,
                            required: false,
                            displayFormat: displayDateFormat
                        }
                    },
                    date_range: function () {
                        return {
                            format: datepickerDateFormat,
                            displayFormat: displayDateFormat,
                            startDate: startDate
                        }
                    }
                },
                'employeeResearchProject[status]': {
                    maxlength: 50
                }
            },
            messages: {
                'employeeResearchProject[name]': {
                    required: lang_nameRequired,
                    maxlength: lang_exceed255Chars
                },
                'employeeResearchProject[role]': {
                    required: lang_roleRequired,
                    maxlength: lang_exceed50Chars
                },
                'employeeResearchProject[start_date]': {
                    required: lang_startDateRequired,
                    valid_date: lang_invalidDate
                },
                'employeeResearchProject[end_date]': {
                    valid_date: lang_invalidDate,
                    date_range: lang_startDateLessEndDate
                },
                'employeeResearchProject[status]': {
                    maxlength: lang_exceed50Chars
                }
            }
        });

    $("#btnResearchProjectCancel").click(function () {
        clearMessageBar();
        if (canEdit) {
            addEditLinks();
        }

        researchProjectValidator.resetForm();

        $('div#changeResearchProject label.error').hide();

        $(".chkbox1").removeAttr("checked");

        //hiding action button section
        $("#actionResearchProject").show();

        $("#changeResearchProject").hide();
        $("#researchProjectRequiredNote").hide();

        $(".chkbox1").show();
        $("#researchProjectCheckAll").show();
    });

    $('form#frmDelResearchProject table a.edit').live('click', function (event) {
        event.preventDefault();

        var seqno = $(this).closest("tr").find('input.chkbox1:first').val();
        clearMessageBar();

        //changing the headings
        $("#headChangeResearchProject").text(lang_editResearchProject);

        $('div#changeResearchProject label.error').hide();

        //hiding action button section
        $("#actionResearchProject").hide();

        //show add  employeeResearchProject form
        $("#changeResearchProject").show();

        $("#employeeResearchProject_seqno").val(seqno);
        $("#employeeResearchProject_name").val($("#name_" + seqno).val());
        $("#employeeResearchProject_role").val($("#role_" + seqno).val());
        $("#employeeResearchProject_start_date").val($("#startDate_" + seqno).val());
        $("#employeeResearchProject_end_date").val($("#endDate_" + seqno).val());
        $("#employeeResearchProject_status").val($("#status_" + seqno).val());

        if ($("#employeeResearchProject_start_date").val() == '') {
            $("#employeeResearchProject_start_date").val(displayDateFormat);
        }
        if ($("#employeeResearchProject_end_date").val() == '') {
            $("#employeeResearchProject_end_date").val(displayDateFormat);
        }

        $("#researchProjectRequiredNote").show();

        $(".chkbox1").hide();
        $("#researchProjectCheckAll").hide();
    });

});

function fillDataToResearchProjectDataPane(seqno) {
    return false;
}
//]]>
</script>